package com.appspot.bs24event.controller.member.photo;

import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.datanucleus.util.StringUtils;
import org.slim3.controller.BaseController;
import org.slim3.controller.Navigation;

import com.appspot.bs24event.util.CommonUtil;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;

/**
 * PhotoListControllerクラス。
 * @version $Id: PhotoListController.java 166 2011-08-11 03:42:48Z bs24event@gmail.com $
 */
public class PhotoListController extends BaseController {

    /** ログ */
    protected static final Logger logger = Logger.getLogger(PhotoListController.class.getName());

    /** リソースバンドル */
    private static ResourceBundle settingProperties = ResourceBundle.getBundle("setting");

    @Override
    protected Navigation setUp() {
        logger.info(this.getClass().getName() + " start.");
        return super.setUp();
    }

    @Override
    protected void tearDown() {
        logger.info(this.getClass().getName() + " end.");
        super.tearDown();
    }

    @Override
    public Navigation run() throws Exception {
        // リクエストパラメーター取得
        String paramAlbumid = CommonUtil.replaceNull(request.getParameter("albumid"));

        // プロパティ値を取得
        String applicationName   = settingProperties.getString("application.name");
        String googleAccountId   = settingProperties.getString("google.account.id");
        String googleAccountPass = settingProperties.getString("google.account.pass");
        String photoFeedUrl      = settingProperties.getString("photo.feed.url");

        try {
            // Picasaウェブアルバムに接続
            PicasawebService myService = new PicasawebService(applicationName);
            myService.setUserCredentials(googleAccountId, googleAccountPass);
            URL feedUrl = new URL(photoFeedUrl + paramAlbumid);
            AlbumFeed feed = myService.getFeed(feedUrl, AlbumFeed.class);

            // 表示用List
            List<Map<String,String>> outputList = new LinkedList<Map<String,String>>();
            for ( PhotoEntry photo : feed.getPhotoEntries() ) {
                Map<String,String> tempMap = new HashMap<String,String>();
                String thumbMbUrl = photo.getMediaThumbnails().get(0).getUrl();
                String thumbUrl   = photo.getMediaThumbnails().get(1).getUrl();
                String photoMbUrl = photo.getMediaThumbnails().get(2).getUrl();

                tempMap.put("thumbMbUrl", thumbMbUrl);
                tempMap.put("thumbUrl", thumbUrl);
                tempMap.put("photoMbUrl", photoMbUrl);
                tempMap.put("photoUrl", StringUtils.replaceAll(thumbUrl, "/s144/", "/s900/"));
                tempMap.put("photoTitle", photo.getTitle().getPlainText());
                tempMap.put("caption", photo.getSummary().getPlainText());
                outputList.add(tempMap);
            }
            // リクエストアトリビュートの設定
            request.setAttribute("albumTitle", feed.getTitle().getPlainText());
            request.setAttribute("myPhotos", outputList);
            request.setAttribute("myPhotosCnt", feed.getTotalResults());
        } catch (Exception e) {
            logger.warn("Picasaウェブアルバム接続時にエラーが発生しました。",e);
        }

        // 写真一覧画面へ遷移
        return forward(request, "photoList.jsp", true, true);
    }
}
